package thread;

import java.util.Scanner;

public class Demo25 {
    public static void main(String[] args) {

        Object lock = new Object();

        Thread t1 = new Thread(()->{
            try {
                System.out.println("t1 wait 前");
                synchronized (lock) {
                    lock.wait();
                }
                System.out.println("t1 wait 后");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2 = new Thread(()->{
            try {
                System.out.println("t2 wait 前");
                synchronized (lock) {
                    lock.wait();
                }
                System.out.println("t2 wait 后");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t3 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);

//            System.out.println("输入唤醒线程");//随机唤醒t1，t2
//            scanner.next();
//            synchronized (lock) {
//                lock.notify();
//            }
//
//            System.out.println("输入唤醒一个线程");
//            scanner.next();
//            synchronized (lock) {
//                lock.notify();//一次notify（）唤醒一个线程
//            }
//
//            System.out.println("输入唤醒另一个线程");
//            scanner.next();
//            synchronized (lock) {
//                lock.notify();
//            }

            System.out.println("输入唤醒线程");
            scanner.next();
            synchronized (lock) {
                lock.notifyAll();
            }

        });

        t1.start();
        t2.start();
        t3.start();

    }
}
